/* eslint-disable no-restricted-globals */
import React, { Component } from 'react'
import './index.css'

export default class item extends Component {

    state = {mouse:false} 
    //鼠标移入与移出状态发生变化
    handleMouse = (flage)=>{
        return()=>{
            //console.log(flage)
            this.setState({mouse:flage})
        }
    }
    //单选框勾选后状态改变
    handleCheck = (id)=>{
        return(event)=>{
            this.props.updatetodo(id,event.target.checked)
        }
    }
    //删除一个todo操作
    handleDelete = (id)=>{
        if(confirm('确定删除吗？')){
            this.props.deletetodo(id)
        }
    }
    render() {
        const {id,name,done} = this.props
        const {mouse} = this.state
        return (
            <li style={{backgroundColor:mouse? '#ddf':'white'}} onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)}>
                <label>
                    <input type="checkbox" checked={done} onChange={this.handleCheck(id) } className="item-checkbox"/>
                    <span>{name}</span>
                </label>
                <button onClick={()=>{this.handleDelete(id)}} className="btn btn-danger" style={{display: mouse? 'block':'none'}}>删除</button>
            </li>
        )
    }
}
